package com.edu.modules.baseinfo.service;

import com.alibaba.fastjson.JSONObject;
import com.edu.modules.baseinfo.dao.AccountDao;
import com.edu.modules.baseinfo.dao.LessonDao;
import com.edu.modules.baseinfo.dao.SignUpDao;
import com.edu.modules.baseinfo.model.SignUpModel;
import com.edu.modules.baseinfo.model.Student;
import com.edu.modules.baseinfo.vo.LoginResultVo;
import com.edu.modules.baseinfo.vo.ResultVo;
import com.edu.modules.entity.TAccountEntity;
import com.edu.modules.entity.TSignUpEntity;
import com.sari.common.service.BaseService;
import com.sari.common.utils.DateUtils;
import com.sari.common.utils.IdGen;
import com.sari.common.utils.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kwj on 2016/7/15.
 */
@Service
public class SignUpService extends BaseService {

	private static Logger logger = LoggerFactory.getLogger(SignUpService.class);

	@Autowired
	private LessonDao lessonDao;

	@Autowired
	private SignUpDao signUpDao;

	public ResultVo signUp(JSONObject param){

		String msg = null;
		boolean flag = false;

		String studentId = param.getString("studentId");
		String lessonId = param.getString("lessonId");
		int count = signUpDao.getCount(studentId,lessonId);
		if (count==1){
			msg = "已经报过该课程";
			flag = false;
			return new ResultVo(flag,msg);
		}

		TSignUpEntity signUp = new TSignUpEntity();
		signUp.setSignUpId(IdGen.uuid());
		signUp.setLessonId(param.getString("lessonId"));
		signUp.setStudentId(param.getString("studentId"));
		signUp.setIsPaid(0);
		signUp.setPayDate("2018");
		signUp.setCreateTime(DateUtils.getDateTime());
		saveObject(signUp);

		int state = lessonDao.updateSignNumber(param.getString("lessonId"));
		if(state == 1){
			msg = "报名成功";
			flag = true;
		}else{
			msg = "报名失败";
			flag = false;
		}
		return new ResultVo(flag,msg);
	}

	public JSONObject getSignUpList(JSONObject param) {

		int offset = param.getInteger("offset");
		int limit = param.getInteger("limit");
		String studentPhone = param.getString("studentPhone");
		int isPaid = param.getInteger("isPaid");

		List list = new ArrayList();
		list = signUpDao.getSignUpList(studentPhone,isPaid,offset,limit);

		JSONObject result= new JSONObject();
		result.put("total",signUpDao.getSignUpCounts(studentPhone,isPaid));
		result.put("rows", list);

		return result;
	}

	public ResultVo updatePaidStatus(String id,String isPaid){

		String msg = null;
		boolean flag = false;

		int status = signUpDao.updatePaidStatus(id,Integer.valueOf(isPaid),DateUtils.getDateTime());
		if(status == 1){
			msg = "操作成功";
			flag = true;
		}else{
			msg = "操作失败";
			flag = false;
		}
		return new ResultVo(flag,msg);
	}
}
